package com.abc.recycling.service.impl;

import com.abc.recycling.domain.SysLog;
import com.abc.recycling.mapper.SysLogMapper;
import com.abc.recycling.service.SysLogService;
import com.abc.recycling.util.IpUtils;
import com.abc.recycling.util.ServletUtils;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SysLogServiceImpl implements SysLogService {

    private final SysLogMapper logMapper;
    
    @Override
    @Async
    public Long recordLog(SysLog log) {
        // 设置操作时间
        log.setOperateTime(LocalDateTime.now());
        
        // 获取请求信息
        HttpServletRequest request = ServletUtils.getRequest();
        if (request != null) {
            // 设置请求方式
            log.setRequestMethod(request.getMethod());
            // 设置请求URL
            log.setRequestUrl(request.getRequestURI());
            // 设置IP地址
            log.setOperateIp(IpUtils.getIpAddr(request));
            // 设置IP地理位置
            log.setOperateLocation(IpUtils.getIpLocation(log.getOperateIp()));
            
            // 获取浏览器和操作系统信息
            String userAgent = request.getHeader("User-Agent");
            UserAgent agent = UserAgent.parseUserAgentString(userAgent);
            log.setBrowser(agent.getBrowser().getName());
            log.setOs(agent.getOperatingSystem().getName());
        }
        
        logMapper.insert(log);
        return log.getId();
    }
    
    @Override
    public SysLog getLog(Long id) {
        return logMapper.findById(id);
    }
    
    @Override
    public List<SysLog> searchLogs(String title, String type, String username, Integer status, 
                                  LocalDateTime startTime, LocalDateTime endTime) {
        return logMapper.search(title, type, username, status, startTime, endTime);
    }
    
    @Override
    @Transactional
    public int cleanLogsBefore(LocalDateTime time) {
        return logMapper.cleanBefore(time);
    }
    
    @Override
    @Transactional
    public int deleteLogs(List<Long> ids) {
        return logMapper.deleteByIds(ids);
    }
    
    @Override
    public int countLogs(String type, Integer status, LocalDateTime startTime, LocalDateTime endTime) {
        return logMapper.count(type, status, startTime, endTime);
    }
    
    @Override
    @Async
    public void recordLoginLog(String username, Integer status, String message) {
        SysLog log = new SysLog();
        log.setTitle("用户登录");
        log.setType("LOGIN");
        log.setMethod("login");
        log.setUsername(username);
        log.setStatus(status);
        log.setErrorMsg(message);
        recordLog(log);
    }
    
    @Override
    @Async
    public void recordOperateLog(String title, String username, Integer status, String message) {
        SysLog log = new SysLog();
        log.setTitle(title);
        log.setType("OPERATE");
        log.setUsername(username);
        log.setStatus(status);
        log.setErrorMsg(message);
        recordLog(log);
    }
    
    @Override
    @Async
    public void recordErrorLog(String title, String username, String errorMsg) {
        SysLog log = new SysLog();
        log.setTitle(title);
        log.setType("ERROR");
        log.setUsername(username);
        log.setStatus(0);
        log.setErrorMsg(errorMsg);
        recordLog(log);
    }
} 